package com.xyzisme76.application;

import com.xyzisme76.dao.CourseDao;
import com.xyzisme76.dao.ScDao;
import com.xyzisme76.dao.StudentDao;
import com.xyzisme76.pojo.Course;
import com.xyzisme76.pojo.Sc;
import com.xyzisme76.pojo.Student;
import com.xyzisme76.utils.MybatisUtil;
import org.apache.ibatis.session.SqlSession;

import javax.imageio.ImageIO;
import javax.swing.*;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableModel;
import javax.swing.table.TableRowSorter;
import java.awt.*;
import java.io.File;
import java.io.IOException;
import java.util.Iterator;
import java.util.List;

public class SqlSelect {
    private static final int WIDTH =800;
    private static final int HEIGHT =600;
    SqlSelect(){
        sqlSelectInfo();
    }

    void sqlSelectInfo() {
        JFrame jf = new JFrame("sql");
        jf.setSize(new Dimension(WIDTH,HEIGHT));
        //jf.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);

        // 创建内容面板，使用边界布局
        //JPanel panel = new JPanel(new BorderLayout());
        // 所在位置 居中
        jf.setLocationRelativeTo(null);
        //设置背景图片
        JPanel jp = new JPanel() {
            protected void paintComponent(Graphics g) {
                try {
                    Image bg = ImageIO.read(new File(
                            "src/main/java/com/xyzisme76/application/images/searchback.png"));
                    g.drawImage(bg, 0, 0, getWidth(), getHeight(), null);
                }catch(IOException e) {
                    e.printStackTrace();
                }
            }
        };
        //初始化显示学生信息表，后期会刷新表单

        // 表头（列名）


        List<Student> students = StudentInfo.studentsGet();
        Iterator var4 = students.iterator();
        int forNut = students.size();
        Object[] columnNames = {"学号", "姓名", "性别", "年龄", "所在系"};
        Object[][] rowData = new Object[forNut][5];// 表格所有行数据
        int j = 0;
        while(var4.hasNext()) {
            Student student = (Student)var4.next();
            rowData[j][0] = student.getSno();
            rowData[j][1] = student.getName();
            rowData[j][2] = student.getSex();
            rowData[j][3] = student.getAge();
            rowData[j][4] = student.getDept();
            j++;
        }
        // 创建 表格模型，指定 所有行数据 和 表头
        TableModel tableModel = new DefaultTableModel(rowData, columnNames);

        // 使用 表格模型 创建 表格
        JTable table = new JTable(tableModel);
        JButton jCourse = new JButton("课程表");
        JButton jSc = new JButton("成绩表");
        JButton jTittle = new JButton("学生信息表");
        JButton jSelect = new JButton("查询");

        // 使用 表格模型 创建 行排序器（TableRowSorter 实现了 RowSorter）
        //RowSorter<TableModel> rowSorter = new TableRowSorter<TableModel>(tableModel);

        // 给 表格 设置 行排序器
        //table.setRowSorter(rowSorter);

        // 把 表头 添加到容器顶部（使用普通的中间容器添加表格时，表头 和 内容 需要分开添加）
        jp.add(table.getTableHeader());
        // 把 表格内容 添加到容器中心
        jp.add(table);
        jp.add(jTittle);
        //jp.add(jStudent);
        jp.add(jCourse);
        jp.add(jSc);
        jp.add(jSelect);
        jp.setLayout(null);

        //布局太麻烦了，直接绝对位置
        jCourse.setForeground(Color.LIGHT_GRAY);
        jCourse.setFont(new Font("微软雅黑", Font.BOLD, 30));
        jTittle.setForeground(Color.DARK_GRAY);
        jTittle.setFont(new Font("微软雅黑", Font.BOLD, 30));
        jSc.setForeground(Color.LIGHT_GRAY);
        jSc.setFont(new Font("微软雅黑", Font.BOLD, 30));
        jSelect.setForeground(Color.LIGHT_GRAY);
        jSelect.setFont(new Font("微软雅黑", Font.BOLD, 30));
        //将 button 完全透明



        jCourse.setBackground(Color.white);
        jCourse.setMargin(new Insets(0,0,0,0));//将边框外的上下左右空间设置为0
        jCourse.setBorderPainted(false);//不打印边框
        jCourse.setFocusPainted(false);//除去焦点的框
        jCourse.setContentAreaFilled(false);//除去默认的背景填充
        jCourse.addActionListener(e -> {
            List<Course> courseList = CourseInfo.CourseGet();
            Iterator var5 = courseList.iterator();
            int forNut2 = courseList.size();
            Object[] columnNames2 = {"课程号", "课程名", "学分", "教师"};
            Object[][] rowData2 = new Object[forNut2][4];// 表格所有行数据
            int i = 0;
            while(var5.hasNext()) {
                Course course = (Course) var5.next();
                rowData2[i][0] = course.getCno();
                rowData2[i][1] = course.getCname();
                rowData2[i][2] = course.getCredit();
                rowData2[i][3] = course.getTeacher();
                i++;
            }
            TableModel tableModels2 = new DefaultTableModel(rowData2, columnNames2);
            table.setModel(tableModels2);
            //table.setEnabled(true);

            jCourse.setForeground(Color.DARK_GRAY);
            jTittle.setForeground(Color.LIGHT_GRAY);
            jSc.setForeground(Color.LIGHT_GRAY);
            jSelect.setForeground(Color.LIGHT_GRAY);
        });

        jTittle.setBackground(Color.white);
        jTittle.setMargin(new Insets(0,0,0,0));//将边框外的上下左右空间设置为0
        jTittle.setBorderPainted(false);//不打印边框
        jTittle.setFocusPainted(false);//除去焦点的框
        jTittle.setContentAreaFilled(false);//除去默认的背景填充
        jTittle.addActionListener(e -> {


            TableModel tableModels = new DefaultTableModel(rowData, columnNames);

            // 使用 表格模型 创建 表格
            table.setModel(tableModels);
            table.setEnabled(true);
            jTittle.setForeground(Color.DARK_GRAY);
            jCourse.setForeground(Color.LIGHT_GRAY);
            jSc.setForeground(Color.LIGHT_GRAY);
            jSelect.setForeground(Color.LIGHT_GRAY);
        });


        jSc.setBackground(Color.white);
        jSc.setMargin(new Insets(0,0,0,0));//将边框外的上下左右空间设置为0
        jSc.setBorderPainted(false);//不打印边框
        jSc.setFocusPainted(false);//除去焦点的框
        jSc.setContentAreaFilled(false);//除去默认的背景填充
        jSc.addActionListener(e -> {

            List<Sc> scList = ScInfo.ScGet();
            Iterator var6 =scList.iterator();
            int forNut3 = scList.size();
            Object[] columnNames3 = {"学生号", "学生名字","课程", "成绩"};
            Object[][] rowData3 = new Object[forNut3][4];// 表格所有行数据
            int f = 0;
            while(var6.hasNext()) {
                Sc sc = (Sc) var6.next();
                rowData3[f][0] = sc.getSno();
                rowData3[f][1] = StudentInfo.stuNameGet(sc.getSno());
                rowData3[f][2] = CourseInfo.courseNameGet(sc.getCno());
                rowData3[f][3] = sc.getGrade();
                f++;
            }

            TableModel tableModels3 = new DefaultTableModel(rowData3, columnNames3);
            table.setModel(tableModels3);
            jSc.setForeground(Color.DARK_GRAY);
            jCourse.setForeground(Color.LIGHT_GRAY);
            jTittle.setForeground(Color.LIGHT_GRAY);
            jSelect.setForeground(Color.LIGHT_GRAY);


        });

        jSelect.setBackground(Color.white);
        jSelect.setMargin(new Insets(0,0,0,0));//将边框外的上下左右空间设置为0
        jSelect.setBorderPainted(false);//不打印边框
        jSelect.setFocusPainted(false);//除去焦点的框
        jSelect.setContentAreaFilled(false);//除去默认的背景填充
        jSelect.addActionListener(e -> {
            jSelect.setForeground(Color.DARK_GRAY);
            jCourse.setForeground(Color.LIGHT_GRAY);
            jTittle.setForeground(Color.LIGHT_GRAY);
            jSc.setForeground(Color.LIGHT_GRAY);

            new SqlSelectInfo();
        });


        table.setBounds(100, 120, 500, 600);
        table.getTableHeader().setBounds(100,90,500,30);
        jTittle.setBounds(60,30,190,60);
        jCourse.setBounds(260,30,150,60);
        jSc.setBounds(425,30,130,60);
        jSelect.setBounds(580,30,100,60);
        jf.setContentPane(jp);

        jf.setLocationRelativeTo(null);
        jf.setVisible(true);

    }

}

class StudentInfo {
    static List<Student> studentsGet(){
        SqlSession sqlSession = MybatisUtil.getSqlSession();
        StudentDao studentDao = (StudentDao)sqlSession.getMapper(StudentDao.class);
        List<Student> studentList = studentDao.getStudentList();
        Iterator var4 = studentList.iterator();

        while(var4.hasNext()) {
            Student student = (Student)var4.next();
            System.out.println(student);
        }
        sqlSession.close();
        return studentList;
    }
    static String stuNameGet(String sno){
        SqlSession sqlSession = MybatisUtil.getSqlSession();
        StudentDao studentDao = (StudentDao)sqlSession.getMapper(StudentDao.class);
        Student student=studentDao.getStudentBySno(sno);
        return student.getName();
    }
}

class CourseInfo {
    static List<Course> CourseGet(){
        SqlSession sqlSession = MybatisUtil.getSqlSession();
        CourseDao CourseDao = (CourseDao)sqlSession.getMapper(CourseDao.class);
        List<Course> courseList = CourseDao.getCourseList();
        Iterator var4 = courseList.iterator();

        while(var4.hasNext()) {
            Course course = (Course)var4.next();
            System.out.println(course);
        }
        sqlSession.close();
        return courseList;
    }
    static String courseNameGet(String cno){
        SqlSession sqlSession = MybatisUtil.getSqlSession();
        CourseDao CourseDao = (CourseDao)sqlSession.getMapper(CourseDao.class);
        Course course=CourseDao.getCourseByCno(cno);
        return course.getCname();
    }
}

class ScInfo{
    static List<Sc> ScGet(){
        SqlSession sqlSession = MybatisUtil.getSqlSession();
        ScDao scDao = (ScDao)sqlSession.getMapper(ScDao.class);
        List<Sc> scList = scDao.getScList();
        Iterator var4 = scList.iterator();
        while(var4.hasNext()) {
            Sc sc = (Sc)var4.next();
            System.out.println(sc);
        }
        sqlSession.close();
        return scList;
    }
    static double ScGetAvg(String cno){
        SqlSession sqlSession = MybatisUtil.getSqlSession();
        ScDao scDao = (ScDao)sqlSession.getMapper(ScDao.class);
        java.text.DecimalFormat df = new java.text.DecimalFormat("#.##");
        if(scDao.getGradeByCno(cno)==null){
            return -1;
        }
        double avg = scDao.getAvg(cno);
        System.out.println(avg);
        sqlSession.close();
        return Double.parseDouble(df.format(avg));
    }
    static List<String> getRank(){
        SqlSession sqlSession = MybatisUtil.getSqlSession();
        ScDao scDao = sqlSession.getMapper(ScDao.class);
        List<String> rank=scDao.getRank();
        return rank;
    }
    static int getGrade(Sc sc){
        SqlSession sqlSession = MybatisUtil.getSqlSession();
        ScDao scDao = sqlSession.getMapper(ScDao.class);
        int score=scDao.getGrade(sc);
        return score;
    }
}